#include <rtthread.h>

#include <io/rttSerial.h>
#include <fport.h>

using namespace NXMFC;

#include <stdio.h>

#define FPORT_DEVNAME ":dev/uart4"

static int fportser(int argc, char **argv)
{
    rttSerial ser;
    char buf[64];
    int ret;
    int cnt = 10000;
    fport fp;

    if (!ser.open(FPORT_DEVNAME))
    {
        printf("open fail\n");
        return -1;
    }

    if (!fp.allocBuf())
    {
        printf("no mem\n");
        return -1;
    }

    ser.setBaudRate(115200);
    ser.setHalfDuplex(true);

    while (cnt -- > 0)
    {
        fport::RecvResult rr;
        int type;

        rr = fp.recv(ser, type, 1000);
        if (rr == fport::FPRR_DONE)
        {
            uint16_t d, raw;

            printf("Recv Done\n");

            for (unsigned i = 0; i < 16; i ++)
            {
                fp.getChannel(i, d, &raw);
                printf("Chn(%d): [0x%04X, 0x%04X]\n", i, d, raw);
            }
        }
    }

    ser.close();

    return 0;
}
MSH_CMD_EXPORT(fportser, test fport serial);
